package com.nbsaas.boot;

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.List;

/**
 * OCR调试测试类
 * 用于测试OCR和AI服务的各个组件
 */
public class OcrDebugTest {
    
    public static void main(String[] args) {
        // 测试OCR连接
        testOcrConnection();
        
        // 测试AI服务
        testAiService();
    }
    
    /**
     * 测试OCR连接和基本功能
     */
    private static void testOcrConnection() {
        System.out.println("=== 测试OCR连接 ===");
        
        try {
            // 初始化OCR客户端
            AipOcr ocrClient = new AipOcr(OcrConfig.BAIDU_APP_ID, OcrConfig.BAIDU_API_KEY, OcrConfig.BAIDU_SECRET_KEY);
            ocrClient.setConnectionTimeoutInMillis(OcrConfig.OCR_CONNECTION_TIMEOUT);
            ocrClient.setSocketTimeoutInMillis(OcrConfig.OCR_SOCKET_TIMEOUT);
            
            // 测试图片URL
            String testImageUrl = "https://baisonscm.oss-cn-beijing.aliyuncs.com/scm/files/aa.png";
            
            System.out.println("正在测试OCR识别...");
            JSONObject result = ocrClient.basicGeneralUrl(testImageUrl, new HashMap<>());
            
            System.out.println("OCR返回结果:");
            System.out.println(result.toString(2));
            
            // 分析结果结构
            System.out.println("\n=== OCR结果分析 ===");
            System.out.println("返回的字段: " + result.keySet());
            
            if (result.has("error_code")) {
                System.out.println("错误码: " + result.getInt("error_code"));
                System.out.println("错误信息: " + result.optString("error_msg", "无"));
            }
            
            if (result.has("words_result")) {
                System.out.println("识别成功，包含words_result字段");
            } else {
                System.out.println("识别失败，不包含words_result字段");
            }
            
        } catch (Exception e) {
            System.err.println("OCR测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    /**
     * 测试AI服务
     */
    private static void testAiService() {
        System.out.println("\n=== 测试AI服务 ===");
        
        try {
            OcrAiService service = new OcrAiService();
            service.initialize();
            
            // 测试AI纠错功能
            List<String> testWords = List.of("13800138000", "14:30去电", "120秒钟");
            System.out.println("测试文字: " + testWords);
            
            // 这里可以添加AI测试逻辑
            System.out.println("AI服务初始化成功");
            
            service.shutdown();
            
        } catch (Exception e) {
            System.err.println("AI服务测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    /**
     * 测试JSON解析
     */
    private static void testJsonParsing() {
        System.out.println("\n=== 测试JSON解析 ===");
        
        // 模拟AI返回的各种JSON格式
        String[] testJsons = {
            "{\"phone\": \"13800138000\", \"callTime\": \"14:30\", \"duration\": \"120\"}",
            "{\"phone\": null, \"callTime\": \"14:30\", \"duration\": 120}",
            "{\"phone\": 13800138000, \"callTime\": \"14:30\", \"duration\": \"120\"}",
            "{\"phone\": \"13800138000\", \"callTime\": null, \"duration\": \"120\"}"
        };
        
        for (int i = 0; i < testJsons.length; i++) {
            System.out.println("测试JSON " + (i + 1) + ": " + testJsons[i]);
            
            try {
                JSONObject json = new JSONObject(testJsons[i]);
                
                // 测试安全解析
                String phone = null;
                String callTime = null;
                String duration = null;
                
                if (json.has("phone") && !json.isNull("phone")) {
                    Object phoneObj = json.get("phone");
                    if (phoneObj != null) {
                        phone = phoneObj.toString();
                    }
                }
                
                if (json.has("callTime") && !json.isNull("callTime")) {
                    Object callTimeObj = json.get("callTime");
                    if (callTimeObj != null) {
                        callTime = callTimeObj.toString();
                    }
                }
                
                if (json.has("duration") && !json.isNull("duration")) {
                    Object durationObj = json.get("duration");
                    if (durationObj != null) {
                        try {
                            if (durationObj instanceof Integer) {
                                duration = durationObj.toString();
                            } else {
                                duration = durationObj.toString();
                            }
                        } catch (Exception e) {
                            System.err.println("时长解析失败: " + durationObj);
                        }
                    }
                }
                
                System.out.println("解析结果 - 手机号: " + phone + ", 时间: " + callTime + ", 时长: " + duration);
                
            } catch (Exception e) {
                System.err.println("JSON解析失败: " + e.getMessage());
            }
        }
    }
} 